matplotlib.ticker 您所在的位置:网站首页 Tick formatters matplotlib.ticker

matplotlib.ticker

2024-02-21 14:10| 来源: 网络整理| 查看: 265

Tick locating and formatting¶

This module contains classes for configuring tick locating and formatting. Generic tick locators and formatters are provided, as well as domain specific custom ones.

Although the locators know nothing about major or minor ticks, they are used by the Axis class to support major and minor tick locating and formatting.

Tick locating¶

The Locator class is the base class for all tick locators. The locators handle autoscaling of the view limits based on the data limits, and the choosing of tick locations. A useful semi-automatic tick locator is MultipleLocator. It is initialized with a base, e.g., 10, and it picks axis limits and ticks that are multiples of that base.

The Locator subclasses defined here are:

AutoLocator MaxNLocator with simple defaults. This is the default tick locator for most plotting. MaxNLocator Finds up to a max number of intervals with ticks at nice locations. LinearLocator Space ticks evenly from min to max. LogLocator Space ticks logarithmically from min to max. MultipleLocator Ticks and range are a multiple of base; either integer or float. FixedLocator Tick locations are fixed. IndexLocator Locator for index plots (e.g., where x = range(len(y))). NullLocator No ticks. SymmetricalLogLocator Locator for use with the symlog norm; works like LogLocator for the part outside of the threshold and adds 0 if inside the limits. AsinhLocator Locator for use with the asinh norm, attempting to space ticks approximately uniformly. LogitLocator Locator for logit scaling. AutoMinorLocator Locator for minor ticks when the axis is linear and the major ticks are uniformly spaced. Subdivides the major tick interval into a specified number of minor intervals, defaulting to 4 or 5 depending on the major interval.

There are a number of locators specialized for date locations - see the dates module.

You can define your own locator by deriving from Locator. You must override the __call__ method, which returns a sequence of locations, and you will probably want to override the autoscale method to set the view limits from the data limits.

If you want to override the default locator, use one of the above or a custom locator and pass it to the x- or y-axis instance. The relevant methods are:

ax.xaxis.set_major_locator(xmajor_locator) ax.xaxis.set_minor_locator(xminor_locator) ax.yaxis.set_major_locator(ymajor_locator) ax.yaxis.set_minor_locator(yminor_locator)

The default minor locator is NullLocator, i.e., no minor ticks on by default.

Note

Locator instances should not be used with more than one ~matplotlib.axis.Axis or ~matplotlib.axes.Axes. So instead of:

locator = MultipleLocator(5) ax.xaxis.set_major_locator(locator) ax2.xaxis.set_major_locator(locator)

do the following instead:

ax.xaxis.set_major_locator(MultipleLocator(5)) ax2.xaxis.set_major_locator(MultipleLocator(5)) Tick formatting¶

Tick formatting is controlled by classes derived from Formatter. The formatter operates on a single tick value and returns a string to the axis.

NullFormatter No labels on the ticks. FixedFormatter Set the strings manually for the labels. FuncFormatter User defined function sets the labels. StrMethodFormatter Use string format method. FormatStrFormatter Use an old-style sprintf format string. ScalarFormatter Default formatter for scalars: autopick the format string. LogFormatter Formatter for log axes. LogFormatterExponent Format values for log axis using exponent = log_base(value). LogFormatterMathtext Format values for log axis using exponent = log_base(value) using Math text. LogFormatterSciNotation Format values for log axis using scientific notation. LogitFormatter Probability formatter. EngFormatter Format labels in engineering notation. PercentFormatter Format labels as a percentage.

You can derive your own formatter from the Formatter base class by simply overriding the __call__ method. The formatter class has access to the axis view and data limits.

To control the major and minor tick label formats, use one of the following methods:

ax.xaxis.set_major_formatter(xmajor_formatter) ax.xaxis.set_minor_formatter(xminor_formatter) ax.yaxis.set_major_formatter(ymajor_formatter) ax.yaxis.set_minor_formatter(yminor_formatter)

In addition to a .Formatter instance, ~.Axis.set_major_formatter and ~.Axis.set_minor_formatter also accept a str or function. str input will be internally replaced with an autogenerated .StrMethodFormatter with the input str. For function input, a .FuncFormatter with the input function will be generated and used.

See /gallery/ticks/major_minor_demo for an example of setting major and minor ticks. See the matplotlib.dates module for more information and examples of using date locators and formatters.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有